<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>登录界面</title>
  <link rel="stylesheet" href="./css/style.css">
  <link rel="stylesheet" href="./css/body.css">
  <script src="../axios.js"></script>
  <script src="../jquery.min.js"></script>
</head>
<body>
  <div class="container">
    <section id="content">
      <form>
        <h1>账号登录</h1></h1>
        <div>
          <i class="close">X</i>
          <input type="text" placeholder="手机号" id="username" name="username">
        </div>
        <div>
          <i class="close">X</i>
          <input type="password" placeholder="密码"  id="password" name="password">
        </div>
        <div>
          <input type="submit" value="登录" class="btn btn-primary" id="js-btn-login">
          <a href="../03-pwdBaseCode/index.html">忘记密码?</a>
          <a href="../02-regbaseCode/reg.html">注册账号</a>
        </div>
      </form>
    </section>
  </div>

  <script>
    //验证用户名
    // function userBtn(){
    //   let username = document.getElementById("username").value
    //   let userReg = /^[a-zA-Z][a-zA-Z0-9]{3,15}$/;
    //   if(userReg.test(username)) return true;

    //   console.log("用户名错")
    //   return false;
    // }

    //密码验证
    // function pwdBtn() {
    //   let pwd = document.getElementById("password").value;
    //   let pwdReg = /^[a-zA-Z0-9]{4,10}$/;

    //   if (pwdReg.test(pwd)) return true;

    //   console.log("密码错")
    //   return false
    // }

    // $(function(){
    //   $('form').on('submit',function(e){
    //     e.preventDefault()
    //     const username = $('#username').val().trim()
    //     const password = $('#password').val().trim()

    //     const sex = 1
    //     const vip = 0

    //     if (userBtn() && pwdBtn() ) {
    //       $.ajax({
    //         method:'post',
    //         url:'http://124.223.14.236:3001/api/user/login',
    //         data:{
    //           username,
    //           password,
    //         },
    //         success:function(res){
    //           console.log(res);
    //           localStorage.setItem("token", res.data.token)
    //         }
    //       })
    //     }
    //   })
    // })
    // 设置请求根路径
    axios.defaults.baseURL = "http://124.223.14.236:3001/api"

    const form = document.querySelector("form")
    form.addEventListener("submit", async function (e) {
      e.preventDefault()

      let username = form.username.value
      let password = form.password.value

        const {data} = await axios.post("/user/login", {
          username,
          password
        })
        console.log(data.msg)
    })
  </script>
</body>
</html>